/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.resource.entity.ResourceQuestion;
import com.lczyfz.istep.modules.resource.entity.ResourceQuestionRepertory;
import com.lczyfz.istep.modules.resource.entity.ResourceRepertory;
import com.lczyfz.istep.modules.resource.service.ResourceQuestionRepertoryService;
import com.lczyfz.istep.modules.resource.service.ResourceQuestionService;
import com.lczyfz.istep.modules.resource.service.ResourceRepertoryService;
import com.lczyfz.istep.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库管理Controller
 * @author dh
 * @version 2017-01-15
 */
@Controller
@RequestMapping(value = "${adminPath}/resource/resourceRepertory")
public class ResourceRepertoryController extends BaseController {

	@Autowired
	private ResourceRepertoryService resourceRepertoryService;
	@Autowired
	private ResourceQuestionService resourceQuestionService;
	@Autowired
	private ResourceQuestionRepertoryService resourceQuestionRepertoryService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public ResourceRepertory get(@RequestParam(required=false) String id) {
		ResourceRepertory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = resourceRepertoryService.get(id);
		}
		if (entity == null){
			entity = new ResourceRepertory();
		}
		return entity;
	}
	
	@RequiresPermissions("resource:resourceRepertory:view")
	@RequestMapping(value = {"list", ""})
	public String list(ResourceRepertory resourceRepertory, HttpServletRequest request, HttpServletResponse response, Model model) {

		if(systemService.isSuperAdmin())
		{
			model.addAttribute("isSuperAdmin", "true");

		}
		else
		{
			model.addAttribute("isSuperAdmin", "false");
		}
		Page<ResourceRepertory> page = resourceRepertoryService.findPage(new Page<ResourceRepertory>(request, response), resourceRepertory);
		model.addAttribute("page", page);
		return "modules/resource/resourceRepertoryList";
	}

	//根据题库属性获取题目
	@ResponseBody
	@RequestMapping(value = "findRepertoryListByAttribute")
	public JsonModel findRepertoryListByAttribute(@RequestParam(required = false) String attribute) {
		logger.debug("==dingh==" + attribute);

		JsonModel jsonModel = new JsonModel();
		ResourceQuestion resourceQuestion=new ResourceQuestion();
		resourceQuestion.setQuestionAttribute(attribute);
//		resourceCourseware.setCoursewareAttribute(attribute);
		List<ResourceQuestion> resourceQuestionList =resourceQuestionService.findList(resourceQuestion);
//		List<ResourceCourseware> resourceCoursewareList=resourceCoursewareService.findList(resourceCourseware);
//		logger.debug("==dingh==" + resourceCoursewareList.size());
		jsonModel.setDataObj(resourceQuestionList);
		return jsonModel;
	}
	@RequiresPermissions("resource:resourceRepertory:view")
	@RequestMapping(value = "form")
	public String form(ResourceRepertory resourceRepertory, Model model, String type) {
		ResourceQuestion resourceQuestion = new ResourceQuestion();
		List<ResourceQuestion> resourceQuestionList = resourceQuestionService.findList(resourceQuestion);
		int count = 0;
		if (resourceRepertory != null && resourceRepertory.getId() != null && !resourceRepertory.getId().equals("")) {
			logger.debug("题库id [{}]", resourceRepertory.getId());
			ResourceQuestionRepertory resourceQuestionRepertory = new ResourceQuestionRepertory();
			resourceQuestionRepertory.setRepertoryId(resourceRepertory.getId());
			List<ResourceQuestionRepertory> resourceQuestionRepertoryList = resourceQuestionRepertoryService.findList(resourceQuestionRepertory);

			for (int i = 0; i < resourceQuestionList.size(); i++) {
				if (resourceQuestionRepertoryList.size() == 0) {
					break;
				} else {
					for (int j = 0; j < resourceQuestionRepertoryList.size(); j++) {
						if (resourceQuestionList.get(i).getId().equals(resourceQuestionRepertoryList.get(j).getQuestionId())) {
							resourceQuestionList.get(i).setIsChoose("true");
							count++;
							resourceQuestionRepertoryList.remove(j);
							break;
						}
					}
				}

			}
		}
		if (type != null && type.equals("copy")) {
			logger.debug("type=======: [{}]",type);
			resourceRepertory.setRepertoryName(resourceRepertory.getRepertoryName() + "-副本");
		}
		if (systemService.isSuperAdmin()) {
			model.addAttribute("isSuperAdmin", "true");
		} else
			model.addAttribute("isSuperAdmin", "false");
		logger.debug("count: [{}]", count + "");
		model.addAttribute("resourceQuestionList", resourceQuestionList);
		logger.debug("resourceQuestionList: [{}]", resourceQuestionList.size() + "");
		model.addAttribute("type", type);
		model.addAttribute("resourceRepertory",resourceRepertory );
		return "modules/resource/resourceRepertoryForm";
	}

	@RequiresPermissions("resource:resourceRepertory:edit")
	@RequestMapping(value = "save")
	public String save(ResourceRepertory resourceRepertory, Model model, RedirectAttributes redirectAttributes, String duallist,String type) {
		if (!beanValidator(model, resourceRepertory)) {
			return form(resourceRepertory, model, null);
		}
		logger.debug("type: [{}]", type);

		if (type != null && type.equals("copy")) {
			logger.debug("复制题库！");
			ResourceRepertory temp = resourceRepertoryService.findRepertoryByRepertoryName(resourceRepertory);
//				if (temp != null && temp.getCourseName().equals(resourceCourse.getCourseName())) {
//					addMessage(redirectAttributes, "复制课程'" + resourceCourse.getCourseName() + "'失败，该课程已存在")}
			if (temp != null && temp.getRepertoryName().equals(resourceRepertory.getRepertoryName())) {
				addMessage(redirectAttributes, "复制题库" + resourceRepertory.getRepertoryName() + "失败，该题库已存在");
			} else {
				if (duallist == null || duallist.equals("")) {
					addMessage(redirectAttributes, "复制题库失败，请选择需要的题目");
				} else {
					ResourceRepertory resourceRepertory1 = new ResourceRepertory();
					resourceRepertory1.setRepertoryName(resourceRepertory.getRepertoryName());
					resourceRepertory1.setRepertoryAttribute(resourceRepertory.getRepertoryAttribute());
					resourceRepertory1.setRemarks(resourceRepertory.getRemarks());
					resourceRepertoryService.save(resourceRepertory1);
					resourceQuestionRepertoryService.save(duallist, resourceRepertory1);
					addMessage(redirectAttributes, "复制题库'" + resourceRepertory.getRepertoryName() + "'成功");
				}
			}
		} else {
			if (resourceRepertoryService.get(resourceRepertory) == null) {
				logger.debug("插入题库！");
//				resourceCtfSet.setSubjectSum(String.valueOf(duallist.split(",").length));
//				resourceRepertory.setQuestionSum(String.valueOf(duallist.split(",").length));

				ResourceRepertory temp = resourceRepertoryService.findRepertoryByRepertoryName(resourceRepertory);
				if (temp != null && temp.getRepertoryName().equals(resourceRepertory.getRepertoryName())) {
					addMessage(redirectAttributes, "添加题库'" + resourceRepertory.getRepertoryName() + "'失败，该题库已存在");
				} else {
					if (duallist == null || duallist.equals("")) {
						addMessage(redirectAttributes, "添加题库失败，请选择需要的题目");
					} else {

						resourceRepertoryService.save(resourceRepertory);
						logger.debug("duallist [{}]", duallist);
						logger.debug("题库id [{}]", resourceRepertory.getId());
						logger.debug("中间表服务 [{}]", resourceQuestionRepertoryService);
						resourceQuestionRepertoryService.save(duallist, resourceRepertory);
						addMessage(redirectAttributes, "添加课件'" + resourceRepertory.getRepertoryName() + "'成功");
					}
				}
			} else {
				ResourceRepertory temp = resourceRepertoryService.findRepertoryByRepertoryName(resourceRepertory);
				if(temp!=null&&!temp.getId().equals(resourceRepertory.getId()))
				{
					addMessage(redirectAttributes,"修改题库失败，该题库已存在");
					return "redirect:" + Global.getAdminPath() + "/resource/resourceRepertory/?repage";
				}

				if (duallist == null || duallist.equals("")) {
					addMessage(redirectAttributes, "修改题库失败，请选择需要的题目");
				} else {
//					resourceRepertory.setQuestionSum(String.valueOf(duallist.split(",").length));
					resourceRepertoryService.save(resourceRepertory);
					resourceQuestionRepertoryService.update(duallist, resourceRepertory);
					addMessage(redirectAttributes, "修改题库'" + resourceRepertory.getRepertoryName() + "'成功");
				}

			}
		}
//		resourceRepertoryService.save(resourceRepertory);
//		addMessage(redirectAttributes, "保存题库管理成功");
		return "redirect:" + Global.getAdminPath() + "/resource/resourceRepertory/?repage";
	}
	
	@RequiresPermissions("resource:resourceRepertory:edit")
	@RequestMapping(value = "delete")
	public String delete(ResourceRepertory resourceRepertory, RedirectAttributes redirectAttributes) {
		resourceRepertoryService.delete(resourceRepertory);
		addMessage(redirectAttributes, "删除题库管理成功");
		return "redirect:"+Global.getAdminPath()+"/resource/resourceRepertory/?repage";
	}


//	查看课程详情
	@RequiresPermissions("resource:resourceRepertory:edit")
	@RequestMapping(value = "detail")
	public String detail(ResourceRepertory resourceRepertory, Model model, HttpServletRequest request, HttpServletResponse response) {
		logger.debug("resourceRepertory=== [{}]", resourceRepertory);
		ResourceQuestion resourceQuestion=new ResourceQuestion();
		resourceQuestion.setRepertoryId(resourceRepertory.getId());
		logger.debug("resourceQuestionId=== [{}]", resourceQuestion.getRepertoryId());

		Page<ResourceQuestion> page = resourceQuestionService.findPageByRepertoryId(new Page<ResourceQuestion>(request, response), resourceQuestion);
		logger.debug("page.getList().size()=== [{}]", page.getList().size());

		model.addAttribute("page", page);
		model.addAttribute("resourceQuestion", resourceQuestion);
		return "modules/resource/resourceRepertoryDetail";
	}
//	public String detail(ResourceRepertory resourceRepertory, RedirectAttributes redirectAttributes) {
////
//		return "modules/resource/resourceRepertoryDetail";
//	}

}